package com.webserver.controller;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.webserver.entity.Article;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ArticleController {
    private static File ARTICLES_DIR=new File("./articles");

    static {
        if(!ARTICLES_DIR.exists()){
            ARTICLES_DIR.mkdirs();
        }
    }

    public void writeArticle(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理发表文章..");

        String title=request.getParameter("title");
        String author=request.getParameter("author");
        String content=request.getParameter("content");
        System.out.println(title+","+author+","+content);
        if (title==null||author==null||content==null){
            File file=new File("./webapps/myweb/article_fail.html");
            response.setContentFile(file);
            return;
        }
        File articlFile=new File(ARTICLES_DIR,".obj");
        if (articlFile.exists()){
            File file=new File("./webapps/myweb/article_fail.html");
            response.setContentFile(file);
            return;
        }
        try (
                ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(articlFile));
              ){
            Article article=new Article(title,author,content);
            oos.writeObject(article);
        } catch (IOException e) {
            e.printStackTrace();
        }
        File file=new File("./webapps/myweb/article_success.html");
        response.setContentFile(file);
        System.out.println("发表文章处理完毕!");

    }

    public  void showAllArticle(HttpServletRequest request,HttpServletResponse response) {
        List<Article> articleList=new ArrayList<>();
        File[] subs=ARTICLES_DIR.listFiles(f->f.getName().endsWith(".obj"));
        for (File articleFile:subs){
            try(
                    ObjectInputStream ois=new ObjectInputStream(new FileInputStream(articleFile));
                    ) {
                Article article=(Article)ois.readObject();
                articleList.add(article);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }

        }
            //2将所有文章拼接到一个html页面中

                    PrintWriter pw =response.getwriter();

                pw.println("<!DOCTYPE html>");
                pw.println("<html lang=\"en\">");
                pw.println("<head>");
                pw.println("<meta charset=\"UTF-8\">");
                pw.println("<title>文章列表</title>");
                pw.println("</head>");
                pw.println("<body>");
                pw.println("<center>");
                pw.println("<h1>文章列表</h1>");
                pw.println("<table border=\"1\">");
                pw.println("<tr>");
                pw.println("<td>标题</td>");
                pw.println("<td>作者</td>");
                pw.println("</tr>");

                for(Article article : articleList) {
                    pw.println("<tr>");
                    pw.println("<td>"+article.getTitle()+"</td>");
                    pw.println("<td>"+article.getAuthor()+"</td>");
                    pw.println("</tr>");
                }

                pw.println("</table>");
                pw.println("</center>");
                pw.println("</body>");
                pw.println("</html>");




            //3将该页面设置到响应对象中发给浏览器
            response.setContentType("text/html");
    }
}
